<?php
/**
 * 验证
 */

namespace Common\Controller\Api;

use Common\Model\TarentoModel;

class CommonController extends SignController
{
    public function __construct()
    {
        $this->checkLogin();
        parent::__construct();

    }
    /**
     * 检查是否有店铺id
     */
    public function checkLogin()
    {
        $member_id = session('member_id');
        if(empty($member_id)){
            $this->ajaxReturn(['code'=>301,'msg'=>'您好，你还未登录，请先登录']);
        }

        if(is_numeric($member_id)){
            $model = new TarentoModel();
            //判断当前分享人是否是首席  红人
            $info = $model->getRow($member_id);  //获取当前登录用户是否是首席 红人

            if($info['level']==1 || $info['level']==2){
                self::$shop_id = $member_id;
            }else{
                $model = new TarentoModel();
                $userinfo = $model->where("member_id=$member_id")->find();
                if($userinfo){
                    $s_id = $userinfo['direct_rec_mid'];
                    $hinfo = $model->where("member_id=$s_id")->find();
                    if($hinfo['level']==3){
                        $this->getDirectSpokesman($s_id);
                    }else{
                        self::$shop_id = $s_id;
                    }
                }

            }
        }
        if(empty(self::$shop_id)){
            self::$shop_id = 14441;
        }
    }

    /**
     * 递归查询上级  一直到获取到代言人(首席)为止
     */
    public function getDirectSpokesman($s_id){
        $model = new TarentoModel();
        $userinfo = $model->where("member_id=$s_id")->find();
        if($userinfo['level']==3){
            $this->getDirectSpokesman($userinfo['direct_rec_mid']);
        }else{
            self::$shop_id =  $userinfo['member_id'];
        }
    }
}